<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin;
use Captcha,Session;

class LoginController extends Controller
{
	
	//登陆
	protected function login(){
		if(request()->isMethod('post')){
			$username	= request('username');
			$password	= request('password');
			$verify		= request('verify');
			if(!Captcha::check($verify)){
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> '验证码错误！',
					'url'	=> ''
					));	
			}
			$res = Admin::where('username',$username)->where('password',md5($password))
				->select('id','username')->first();
			if($res){
				Session::put('user',$res);
				return response()->json(array(
					'code'	=> 1,
					'msg'	=> '登陆成功',
					'url'	=> url('admin/index')
					));
			}else{
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> '登陆失败,账户或密码错误！',
					'url'	=> ''
					));			
			}			
		}
		
		return view('admin/login/login');
	}
	
	//登出
	protected function logout(){
		Session::forget('user');
		return redirect('admin/login');
	}
}
